//
//  FMDeviceDiskTool.m
//  flutter_device_info_plugin
//
//  Created by 小琦 on 2025/2/24.
//

#import "FMDeviceDiskTool.h"
#import <sys/mount.h>
#include <mach/mach.h>

@implementation FMDeviceDiskTool

+ (NSString *)getFMTotalDiskSize {
    struct statfs buf;
    unsigned long long size = -1;
    if (statfs("/var", &buf) >= 0) {
        size = (unsigned long long)(buf.f_bsize * buf.f_blocks);
    }
    return [self sizeToString:size];
}

+ (nonnull NSString *)getFMTotalMemorySize {
    long long size = [NSProcessInfo processInfo].physicalMemory;
    return [self sizeToString:size];
}

+ (nonnull NSString *)getFMAvailableDiskSize {
    NSError *error = nil;
    
    NSDictionary *results = [[[NSURL alloc] initFileURLWithPath:NSTemporaryDirectory()] resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:&error];
    
    if(error){return @"-1";}

    NSString *value = [NSString stringWithFormat:@"%@",results[NSURLVolumeAvailableCapacityForImportantUsageKey]];
    
    return value;
}


+ (nonnull NSString *)getFMAvailableMemorySize {
    vm_statistics_data_t vmStats;
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
    if (kernReturn != KERN_SUCCESS) {
        return @"内存查找失败";
    }
    long long size = ((vm_page_size * vmStats.free_count + vm_page_size * vmStats.inactive_count));
    return [self sizeToString:size];
}

+ (NSString *)sizeToString:(long long)size {
    return [NSString stringWithFormat:@"%llu",(size)];
}

@end
